/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package magazzino.test;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import to.pm.entity.catalogo.ProdottoConsumabile;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

import to.pm.catalogo.bi.GestioneCatalogoBusiness;

/**
 *
 * @author sp043784
 */
public class Prova extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */


    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

            String jndiName = request.getParameter("jndiName");

            if (jndiName == null) {
                jndiName = "to.pm.catalogo.bi.GestioneCatalogoBusiness";
            }

            GestioneCatalogoBusiness mgz =lookupAzioniMagazziniereBean(jndiName);

            List<ProdottoConsumabile> lp = (List<ProdottoConsumabile>) mgz.findProdottiByDescrizione("matite");

            Iterator<ProdottoConsumabile> i=lp.iterator();
            while (i.hasNext()) {
                 Logger.getAnonymousLogger().log(Level.INFO, i.next().toString());
            }

        
        return mapping.findForward(SUCCESS);
    }



    private GestioneCatalogoBusiness lookupAzioniMagazziniereBean(String jndiName) {
        try {
            Context c = new InitialContext();
            return (GestioneCatalogoBusiness) c.lookup(jndiName);
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
